package com.toubutu.audio.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.toubutu.audio.common.BusinessConstants;
import com.toubutu.audio.dto.DoCollectDto;
import com.toubutu.audio.dto.PageQueryCollectionDto;
import com.toubutu.audio.entity.Collection;
import com.toubutu.audio.mapper.CollectionMapper;
import com.toubutu.audio.service.CollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toubutu.audio.service.VideoService;
import com.toubutu.audio.vo.CollectionVo;
import com.toubutu.audio.vo.VideoListItemVo;
import com.toubutu.common.AuthLoginUserInfo;
import com.toubutu.common.CurrentUserContext;
import com.toubutu.common.PageUtils;
import com.toubutu.constants.BaseConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戒躁
 * @since 2024-02-26
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {
    @Resource
    private VideoService videoService;
    @Override
    public PageUtils<CollectionVo> pageQueryCollection(PageQueryCollectionDto dto) {
        LambdaQueryWrapper<Collection> wrapper = dto.getWrapper();
        Long userId = Long.parseLong(CurrentUserContext.getUserId());
        wrapper.eq(Collection::getUserId,userId);
        Page<Collection> page = dto.getPage();
        page = page(page,wrapper);
        List<Collection> records = page.getRecords();
        if (records.isEmpty()){
            return new PageUtils<>(Collections.emptyList(),page);
        }
        List<String> videoCodeList = records.stream().map(Collection::getVideoCode).collect(Collectors.toList());
        Map<String, VideoListItemVo> videoMapInfo = videoService.getVideoItemListByVideoCode(videoCodeList).stream()
                .collect(Collectors.toMap(VideoListItemVo::getVideoCode, c -> c, (o, n) -> n));
        List<CollectionVo> result = records.stream().map(r -> {
            CollectionVo collectionVo = new CollectionVo();
            BeanUtils.copyProperties(r, collectionVo);
            collectionVo.setVideo(videoMapInfo.get(r.getVideoCode()));
            return collectionVo;
        }).collect(Collectors.toList());
        return new PageUtils<>(result,page);
    }

    @Override
    public void doCollect(DoCollectDto doCollectDto) {
        AuthLoginUserInfo currentUser = CurrentUserContext.getCurrentUser();
        Long userId = Long.parseLong(currentUser.getId());
        switch (doCollectDto.getStatus()){
            case BusinessConstants.DO_COLLECT:
                Collection collection = new Collection();
                collection.setUserId(userId).setVideoCode(doCollectDto.getVideoCode()).setIsDelete(BaseConstants.UN_DELETED);
                try {
                    save(collection);
                }catch (DuplicateKeyException exception){
                    LambdaUpdateWrapper<Collection> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.eq(Collection::getUserId,userId)
                            .eq(Collection::getVideoCode,doCollectDto.getVideoCode())
                            .set(Collection::getIsDelete,BaseConstants.UN_DELETED);
                    update(updateWrapper);
                    log.error("DuplicateKeyException处理");
                }
                break;
            case BusinessConstants.CANCEL_COLLECT:
                LambdaUpdateWrapper<Collection> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(Collection::getUserId,userId)
                        .eq(Collection::getVideoCode,doCollectDto.getVideoCode())
                        .set(Collection::getIsDelete,BaseConstants.DELETED);
                update(updateWrapper);
                break;
        }
    }

    @Override
    public Boolean isCollect(Long userId, String videoCode) {
        LambdaQueryWrapper<Collection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collection::getUserId,userId).eq(Collection::getVideoCode,videoCode)
                .eq(Collection::getIsDelete, BaseConstants.UN_DELETED);
        return getOne(wrapper)!=null;
    }
}
